package com.bwie.controller;

import com.bwie.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: lrz
 * @Date: 2024/10/22 18:12
 * @Description:
 **/
@RestController
@RequestMapping("/user/test")
@Slf4j
public class TestController {

    @Autowired
    KafkaTemplate kafkaTemplate;

    //发送
    @PostMapping("/sendMessage/{msg}")
    public R sendMessage(@PathVariable("msg")String msg){
        kafkaTemplate.send("topic-test",msg);
        return R.OK();
    }

    @KafkaListener(topics = "topic-test")
    public void receMessage(String message, Acknowledgment acknowledgment){
        System.out.println("接受消息"+message);
        //手动确认消息
        acknowledgment.acknowledge();;
    }

}
